{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-04T11:16:17.406275Z",
     "iopub.status.busy": "2022-05-04T11:16:17.406275Z",
     "iopub.status.idle": "2022-05-04T11:16:19.379126Z",
     "shell.execute_reply": "2022-05-04T11:16:19.378152Z",
     "shell.execute_reply.started": "2022-05-04T11:16:17.406275Z"
    },
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "欢迎使用缠论数据计算工具；\n",
      "项目地址：https://github.com/yijixiuxin/chanlun-pro\n",
      "授权许可有效期：2099-01-01\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "在使用天勤量化之前，默认您已经知晓并同意以下免责条款，如果不同意请立即停止使用：https://www.shinnytech.com/blog/disclaimer/\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from chanlun.backtesting import backtest\n",
    "from chanlun.strategy import strategy_demo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 注意事项\n",
    "回测前要确保已经同步回测时间段内的行情数据到数据库中\n",
    "\n",
    "同步行情数据请参考 /script/crontab/ 目录下的脚本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-04T11:16:19.382129Z",
     "iopub.status.busy": "2022-05-04T11:16:19.381127Z",
     "iopub.status.idle": "2022-05-04T11:16:19.394893Z",
     "shell.execute_reply": "2022-05-04T11:16:19.393709Z",
     "shell.execute_reply.started": "2022-05-04T11:16:19.382129Z"
    },
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 量化配置\n",
    "bt_config = {\n",
    "    # 策略结果保存的文件\n",
    "    'save_file': './data/bk/futures_strategy_demo.pkl',\n",
    "    # 设置策略对象\n",
    "    'strategy': strategy_demo.StrategyDemo(),\n",
    "    # 回测模式：signal 信号模式，固定金额开仓； trade 交易模式，按照实际金额开仓\n",
    "    'mode': 'signal',\n",
    "    # 市场配置，currency 数字货币  a 沪深  hk  港股  futures  期货\n",
    "    'market': 'futures',\n",
    "    # 基准代码，用于获取回测的时间列表\n",
    "    'base_code': 'SHFE.ag2206',\n",
    "    # 回测的标的代码\n",
    "    'codes': ['SHFE.ag2206'],\n",
    "    # 回测的周期，这里设置里，在策略中才能取到对应周期的数据\n",
    "    'frequencys': ['5m'],\n",
    "    # 回测开始的时间\n",
    "    'start_datetime': '2022-02-01 00:00:00',\n",
    "    # 回测的结束时间\n",
    "    'end_datetime': '2022-03-01 00:00:00',\n",
    "    # 是否是股票，True 当日开仓不可平仓，False 当日开当日可平\n",
    "    'is_stock': False,\n",
    "    # 是否是期货，True 可做空，False 不可做空\n",
    "    'is_futures': True,\n",
    "    # mode 为 trade 生效，初始账户资金\n",
    "    'init_balance': 1000000,\n",
    "    # mode 为 trade 生效，交易手续费率\n",
    "    'fee_rate': 0.0006,\n",
    "    # mode 为 trade 生效，最大持仓数量（分仓）\n",
    "    'max_pos': 2,\n",
    "    # 缠论计算的配置，详见缠论配置说明\n",
    "    'cl_config': {'xd_bzh': 'xd_bzh_no'},\n",
    "}\n",
    "\n",
    "BT = backtest.BackTest(bt_config)\n",
    "BT.datas.load_data_to_cache = True # 是否将所有k线数据加载到内存中，如果内存吃紧，可以设置为 False，这样每次循环都会从数据库中读取\n",
    "BT.datas.load_kline_nums = 10000 # 每次 klines 方法中返回的k线数据条数\n",
    "BT.datas.cl_data_kline_max_nums = 30000 # 回测过程中，如果缠论对象的数据量超过这个值，则会清空重新计算，cl_data_kline_max_nums 不能小于 load_kline_nums，否则将会不断的删除并重新计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-04T11:16:19.395876Z",
     "iopub.status.busy": "2022-05-04T11:16:19.395876Z",
     "iopub.status.idle": "2022-05-04T11:16:27.844583Z",
     "shell.execute_reply": "2022-05-04T11:16:27.843582Z",
     "shell.execute_reply.started": "2022-05-04T11:16:19.395876Z"
    },
    "pycharm": {
     "name": "#%%\n"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[SHFE.ag2206 - 2022-02-07 23:00:00] // 3buy 做多买入（4781.0 - 20.707），原因： 当前级别 (MMD: ['3buy'] Loss: 4767.0) \n",
      "[SHFE.ag2206 - 2022-02-08 09:00:00] // 3buy 平仓做多（4798.0 - 20.707） 盈亏：352.01900000000023  (0.36%)，原因： 3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False, QS: False）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-08 23:00:00] // 3buy 做多买入（4822.0 - 20.5309），原因： 当前级别 (MMD: ['3buy'] Loss: 4783.0) \n",
      "[SHFE.ag2206 - 2022-02-09 00:00:00] // 3buy 平仓做多（4819.0 - 20.5309） 盈亏：-61.59269999999378  (-0.06%)，原因： 3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False, QS: False）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-12 01:00:00] // 3buy 做多买入（4816.0 - 20.5565），原因： 当前级别 (MMD: ['3buy'] Loss: 4799.0) \n",
      "[SHFE.ag2206 - 2022-02-12 01:50:00] // 3buy 平仓做多（4820.0 - 20.5565） 盈亏：82.22600000000966  (0.08%)，原因： 3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False, QS: False）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-14 13:55:00] // 3buy 做多买入（4904.0 - 20.1876），原因： 当前级别 (MMD: ['3buy'] Loss: 4892.0) \n",
      "[SHFE.ag2206 - 2022-02-14 21:05:00] // l3buy 做多买入（4913.0 - 20.1506），原因： 当前级别 (MMD: ['l3buy'] Loss: 4898.0) \n",
      "[SHFE.ag2206 - 2022-02-15 11:20:00] // 3buy 平仓做多（4941.0 - 20.1876） 盈亏：746.9412000000011  (0.75%)，原因： 3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, PZ: False, QS: True）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-15 11:20:00] // l3buy 平仓做多（4941.0 - 20.1506） 盈亏：564.2167999999947  (0.57%)，原因： l3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, PZ: False, QS: True）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-15 11:20:00] // 1sell 做空卖出（4941.0 - 20.0364），原因： 当前级别 (MMD: ['1sell'] Loss: 4964.0) \n",
      "[SHFE.ag2206 - 2022-02-15 22:15:00] // 1sell 平仓做空（4797.0 - 20.0364） 盈亏：2885.2416000000085 (2.91%)，原因： 1sell 当前级别出现买点（MMDS: ['l3buy']，BC：（BI: False, PZ: False, QS: False）），空仓清仓\n",
      "[SHFE.ag2206 - 2022-02-15 22:15:00] // l3buy 做多买入（4797.0 - 20.6379），原因： 当前级别 (MMD: ['l3buy'] Loss: 4780.0) \n",
      "[SHFE.ag2206 - 2022-02-16 00:25:00] // l3buy 平仓做多（4793.0 - 20.6379） 盈亏：-82.55160000000615  (-0.08%)，原因： l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, PZ: False, QS: False）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-16 00:25:00] // 3sell 做空卖出（4793.0 - 20.6551），原因： 当前级别 (MMD: ['3sell'] Loss: 4802.0) \n",
      "[SHFE.ag2206 - 2022-02-16 00:40:00] // l3buy 做多买入（4801.0 - 20.6207），原因： 当前级别 (MMD: ['l3buy'] Loss: 4777.0) \n",
      "[SHFE.ag2206 - 2022-02-16 00:55:00] // 3sell 平仓做空（4804.0 - 20.6551） 盈亏：-227.20610000001034 (-0.23%)，原因： 3sell 止损\n",
      "[SHFE.ag2206 - 2022-02-16 01:15:00] // l3buy 平仓做多（4801.0 - 20.6207） 盈亏：0.0  (0.00%)，原因： l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, PZ: False, QS: False）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-16 01:15:00] // 3sell 做空卖出（4801.0 - 20.6207），原因： 当前级别 (MMD: ['3sell'] Loss: 4808.0) \n",
      "[SHFE.ag2206 - 2022-02-16 02:15:00] // 3sell 平仓做空（4811.0 - 20.6207） 盈亏：-206.20699999999488 (-0.21%)，原因： 3sell 止损\n",
      "[SHFE.ag2206 - 2022-02-17 22:35:00] // 3buy 做多买入（4873.0 - 20.316），原因： 当前级别 (MMD: ['3buy'] Loss: 4857.0) \n",
      "[SHFE.ag2206 - 2022-02-18 09:40:00] // 3buy 平仓做多（4886.0 - 20.316） 盈亏：264.10800000000745  (0.27%)，原因： 3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False, QS: False）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-18 21:50:00] // 2sell 做空卖出（4925.0 - 20.1015），原因： 当前级别 (MMD: ['2sell'] Loss: 4945.0) \n",
      "[SHFE.ag2206 - 2022-02-18 22:45:00] // 2sell 平仓做空（4925.0 - 20.1015） 盈亏：0.0 (0.00%)，原因： 2sell 当前级别出现买点（MMDS: ['3buy']，BC：（BI: False, PZ: False, QS: False）），空仓清仓\n",
      "[SHFE.ag2206 - 2022-02-18 22:45:00] // 3buy 做多买入（4925.0 - 20.1015），原因： 当前级别 (MMD: ['3buy'] Loss: 4905.0) \n",
      "[SHFE.ag2206 - 2022-02-21 09:15:00] // 3buy 平仓做多（4890.0 - 20.1015） 盈亏：-703.5525000000052  (-0.71%)，原因： 3buy 止损\n",
      "[SHFE.ag2206 - 2022-02-21 13:40:00] // 3sell 做空卖出（4877.0 - 20.2994），原因： 当前级别 (MMD: ['3sell'] Loss: 4904.0) \n",
      "[SHFE.ag2206 - 2022-02-21 14:45:00] // 3sell 平仓做空（4873.0 - 20.2994） 盈亏：81.19759999999951 (0.08%)，原因： 3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: False, QS: False）），空仓清仓\n",
      "[SHFE.ag2206 - 2022-02-22 10:55:00] // 3buy 做多买入（4959.0 - 19.9637），原因： 当前级别 (MMD: ['3buy'] Loss: 4916.0) \n",
      "[SHFE.ag2206 - 2022-02-23 09:40:00] // 3buy 平仓做多（4964.0 - 19.9637） 盈亏：99.81849999999395  (0.10%)，原因： 3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False, QS: False）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-24 21:05:00] // 3buy 做多买入（5185.0 - 19.0935），原因： 当前级别 (MMD: ['3buy'] Loss: 5096.0) \n",
      "[SHFE.ag2206 - 2022-02-24 21:50:00] // 3buy 平仓做多（5163.0 - 19.0935） 盈亏：-420.0570000000007  (-0.42%)，原因： 3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False, QS: False）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-25 01:25:00] // l3buy 做多买入（5057.0 - 19.5768），原因： 当前级别 (MMD: ['l3buy'] Loss: 5026.0) \n",
      "[SHFE.ag2206 - 2022-02-25 02:10:00] // l3buy 平仓做多（5049.0 - 19.5768） 盈亏：-156.61440000000584  (-0.16%)，原因： l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, PZ: False, QS: False）），多仓清仓\n",
      "[SHFE.ag2206 - 2022-02-25 02:10:00] // 3sell 做空卖出（5049.0 - 19.6078），原因： 当前级别 (MMD: ['3sell'] Loss: 5061.0) \n",
      "[SHFE.ag2206 - 2022-02-25 14:25:00] // 3sell 平仓做空（4978.0 - 19.6078） 盈亏：1392.1538 (1.41%)，原因： 3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: False, QS: False）），空仓清仓\n",
      "运行完成，执行时间：8.329020023345947\n",
      "save to : ./data/bk/futures_strategy_demo.pkl\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Done\n"
     ]
    }
   ],
   "source": [
    "# 运行回测\n",
    "BT.run()\n",
    "# 多进程回测，只支持信号回测模式\n",
    "# BT.run_process(max_workers=4)\n",
    "# 保存回测结果到文件中\n",
    "BT.save()\n",
    "print('Done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-04T11:16:27.845581Z",
     "iopub.status.busy": "2022-05-04T11:16:27.845581Z",
     "iopub.status.idle": "2022-05-04T11:16:27.875253Z",
     "shell.execute_reply": "2022-05-04T11:16:27.874284Z",
     "shell.execute_reply.started": "2022-05-04T11:16:27.845581Z"
    },
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-05-04 19:16:27\n",
      "Save File : ./data/bk/futures_strategy_demo.pkl\n",
      "Mode signal init balance 1000000 fee rate 0.0006\n",
      "is stock False is futures True\n",
      "STR Class : <chanlun.strategy.strategy_demo.StrategyDemo object at 0x000001FE479AE9C8>\n",
      "Base Code : SHFE.ag2206\n",
      "Run Codes : ['SHFE.ag2206']\n",
      "Frequencys : ['5m']\n",
      "Start time : 2022-02-01 00:00:00 End time : 2022-03-01 00:00:00\n",
      "CL Config : {'xd_bzh': 'xd_bzh_no', 'fx_qj': 'fx_qj_k', 'fx_bh': 'fx_bh_dingdi', 'bi_type': 'bi_type_new', 'bi_bzh': 'bi_bzh_yes', 'bi_qj': 'bi_qj_ck', 'bi_fx_cgd': 'bi_fx_cgd_no', 'xd_qj': 'xd_qj_dd', 'zsd_bzh': 'zsd_bzh_yes', 'zsd_qj': 'zsd_qj_dd', 'zs_bi_type': 'zs_type_dn', 'zs_xd_type': 'zs_type_dn', 'zs_qj': 'zs_qj_dd', 'zs_wzgx': 'zs_wzgx_zgd', 'idx_macd_fast': 12, 'idx_macd_slow': 26, 'idx_macd_signal': 9, 'idx_boll_period': 20, 'idx_ma_period': 5}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "+--------------+------+------+--------+---------+--------+---------+----------+----------+----------+--------+\n",
      "|    买卖点    | 成功 | 失败 |  胜率  |   盈利  |  亏损  |  净利润 | 回吐比例 | 平均盈利 | 平均亏损 | 盈亏比 |\n",
      "+--------------+------+------+--------+---------+--------+---------+----------+----------+----------+--------+\n",
      "|   一类买点   |  0   |  0   |   0%   |    0    |   0    |    0    |    0     |    0     |    0     |   0    |\n",
      "|   二类买点   |  0   |  0   |   0%   |    0    |   0    |    0    |    0     |    0     |    0     |   0    |\n",
      "|  类二类买点  |  0   |  0   |   0%   |    0    |   0    |    0    |    0     |    0     |    0     |   0    |\n",
      "|   三类买点   |  5   |  3   | 62.5%  | 1545.11 | 1185.2 |  359.91 |  76.71   |  309.02  |  395.07  | 0.7822 |\n",
      "|  类三类买点  |  1   |  3   | 25.0%  |  564.22 | 239.17 |  325.05 |  42.39   |  564.22  |  79.72   | 7.0773 |\n",
      "| 下跌盘整背驰 |  0   |  0   |   0%   |    0    |   0    |    0    |    0     |    0     |    0     |   0    |\n",
      "| 下跌趋势背驰 |  0   |  0   |   0%   |    0    |   0    |    0    |    0     |    0     |    0     |   0    |\n",
      "|   一类卖点   |  1   |  0   | 100.0% | 2885.24 |   0    | 2885.24 |   0.0    | 2885.24  |    0     |   0    |\n",
      "|   二类卖点   |  0   |  1   |  0.0%  |    0    |  0.0   |   0.0   |    0     |    0     |   0.0    |   0    |\n",
      "|  类二类卖点  |  0   |  0   |   0%   |    0    |   0    |    0    |    0     |    0     |    0     |   0    |\n",
      "|   三类卖点   |  2   |  2   | 50.0%  | 1473.35 | 433.41 | 1039.94 |  29.42   |  736.68  |  216.71  | 3.3994 |\n",
      "|  类三类卖点  |  0   |  0   |   0%   |    0    |   0    |    0    |    0     |    0     |    0     |   0    |\n",
      "| 上涨盘整背驰 |  0   |  0   |   0%   |    0    |   0    |    0    |    0     |    0     |    0     |   0    |\n",
      "| 上涨趋势背驰 |  0   |  0   |   0%   |    0    |   0    |    0    |    0     |    0     |    0     |   0    |\n",
      "+--------------+------+------+--------+---------+--------+---------+----------+----------+----------+--------+\n"
     ]
    }
   ],
   "source": [
    "# 输出回测结果\n",
    "BT.info()\n",
    "BT.result()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 回测数据展示\n",
    "BT.backtest_charts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-04T11:16:28.154013Z",
     "iopub.status.busy": "2022-05-04T11:16:28.154013Z",
     "iopub.status.idle": "2022-05-04T11:16:28.183526Z",
     "shell.execute_reply": "2022-05-04T11:16:28.182525Z",
     "shell.execute_reply.started": "2022-05-04T11:16:28.154013Z"
    },
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>mmd</th>\n",
       "      <th>open_datetime</th>\n",
       "      <th>close_datetime</th>\n",
       "      <th>type</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>loss_price</th>\n",
       "      <th>profit_rate</th>\n",
       "      <th>max_profit_rate</th>\n",
       "      <th>max_loss_rate</th>\n",
       "      <th>open_msg</th>\n",
       "      <th>close_msg</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3buy</td>\n",
       "      <td>2022-02-07 23:00:00</td>\n",
       "      <td>2022-02-08 09:00:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4781.0</td>\n",
       "      <td>20.7070</td>\n",
       "      <td>4767.0</td>\n",
       "      <td>0.36</td>\n",
       "      <td>0.5438</td>\n",
       "      <td>-0.2510</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4767.0)</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3buy</td>\n",
       "      <td>2022-02-08 23:00:00</td>\n",
       "      <td>2022-02-09 00:00:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4822.0</td>\n",
       "      <td>20.5309</td>\n",
       "      <td>4783.0</td>\n",
       "      <td>-0.06</td>\n",
       "      <td>0.2489</td>\n",
       "      <td>-0.1866</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4783.0)</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3buy</td>\n",
       "      <td>2022-02-12 01:00:00</td>\n",
       "      <td>2022-02-12 01:50:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4816.0</td>\n",
       "      <td>20.5565</td>\n",
       "      <td>4799.0</td>\n",
       "      <td>0.08</td>\n",
       "      <td>0.3530</td>\n",
       "      <td>-0.0623</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4799.0)</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3buy</td>\n",
       "      <td>2022-02-14 13:55:00</td>\n",
       "      <td>2022-02-15 11:20:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4904.0</td>\n",
       "      <td>20.1876</td>\n",
       "      <td>4892.0</td>\n",
       "      <td>0.75</td>\n",
       "      <td>1.2235</td>\n",
       "      <td>-0.1223</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4892.0)</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, PZ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>l3buy</td>\n",
       "      <td>2022-02-14 21:05:00</td>\n",
       "      <td>2022-02-15 11:20:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4913.0</td>\n",
       "      <td>20.1506</td>\n",
       "      <td>4898.0</td>\n",
       "      <td>0.57</td>\n",
       "      <td>1.0381</td>\n",
       "      <td>-0.3053</td>\n",
       "      <td>当前级别 (MMD: ['l3buy'] Loss: 4898.0)</td>\n",
       "      <td>l3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, P...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>1sell</td>\n",
       "      <td>2022-02-15 11:20:00</td>\n",
       "      <td>2022-02-15 22:15:00</td>\n",
       "      <td>做空</td>\n",
       "      <td>4941.0</td>\n",
       "      <td>20.0364</td>\n",
       "      <td>4964.0</td>\n",
       "      <td>2.91</td>\n",
       "      <td>3.2584</td>\n",
       "      <td>-0.2429</td>\n",
       "      <td>当前级别 (MMD: ['1sell'] Loss: 4964.0)</td>\n",
       "      <td>1sell 当前级别出现买点（MMDS: ['l3buy']，BC：（BI: False, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>l3buy</td>\n",
       "      <td>2022-02-15 22:15:00</td>\n",
       "      <td>2022-02-16 00:25:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4797.0</td>\n",
       "      <td>20.6379</td>\n",
       "      <td>4780.0</td>\n",
       "      <td>-0.08</td>\n",
       "      <td>0.1042</td>\n",
       "      <td>-0.4169</td>\n",
       "      <td>当前级别 (MMD: ['l3buy'] Loss: 4780.0)</td>\n",
       "      <td>l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3sell</td>\n",
       "      <td>2022-02-16 00:25:00</td>\n",
       "      <td>2022-02-16 00:55:00</td>\n",
       "      <td>做空</td>\n",
       "      <td>4793.0</td>\n",
       "      <td>20.6551</td>\n",
       "      <td>4802.0</td>\n",
       "      <td>-0.23</td>\n",
       "      <td>0.0209</td>\n",
       "      <td>-0.3130</td>\n",
       "      <td>当前级别 (MMD: ['3sell'] Loss: 4802.0)</td>\n",
       "      <td>3sell 止损</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>l3buy</td>\n",
       "      <td>2022-02-16 00:40:00</td>\n",
       "      <td>2022-02-16 01:15:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4801.0</td>\n",
       "      <td>20.6207</td>\n",
       "      <td>4777.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.1458</td>\n",
       "      <td>-0.0833</td>\n",
       "      <td>当前级别 (MMD: ['l3buy'] Loss: 4777.0)</td>\n",
       "      <td>l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3sell</td>\n",
       "      <td>2022-02-16 01:15:00</td>\n",
       "      <td>2022-02-16 02:15:00</td>\n",
       "      <td>做空</td>\n",
       "      <td>4801.0</td>\n",
       "      <td>20.6207</td>\n",
       "      <td>4808.0</td>\n",
       "      <td>-0.21</td>\n",
       "      <td>0.0417</td>\n",
       "      <td>-0.2499</td>\n",
       "      <td>当前级别 (MMD: ['3sell'] Loss: 4808.0)</td>\n",
       "      <td>3sell 止损</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3buy</td>\n",
       "      <td>2022-02-17 22:35:00</td>\n",
       "      <td>2022-02-18 09:40:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4873.0</td>\n",
       "      <td>20.3160</td>\n",
       "      <td>4857.0</td>\n",
       "      <td>0.27</td>\n",
       "      <td>0.7182</td>\n",
       "      <td>-0.3078</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4857.0)</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>2sell</td>\n",
       "      <td>2022-02-18 21:50:00</td>\n",
       "      <td>2022-02-18 22:45:00</td>\n",
       "      <td>做空</td>\n",
       "      <td>4925.0</td>\n",
       "      <td>20.1015</td>\n",
       "      <td>4945.0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.4061</td>\n",
       "      <td>-0.0609</td>\n",
       "      <td>当前级别 (MMD: ['2sell'] Loss: 4945.0)</td>\n",
       "      <td>2sell 当前级别出现买点（MMDS: ['3buy']，BC：（BI: False, P...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3buy</td>\n",
       "      <td>2022-02-18 22:45:00</td>\n",
       "      <td>2022-02-21 09:15:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4925.0</td>\n",
       "      <td>20.1015</td>\n",
       "      <td>4905.0</td>\n",
       "      <td>-0.71</td>\n",
       "      <td>0.1827</td>\n",
       "      <td>-0.8528</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4905.0)</td>\n",
       "      <td>3buy 止损</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3sell</td>\n",
       "      <td>2022-02-21 13:40:00</td>\n",
       "      <td>2022-02-21 14:45:00</td>\n",
       "      <td>做空</td>\n",
       "      <td>4877.0</td>\n",
       "      <td>20.2994</td>\n",
       "      <td>4904.0</td>\n",
       "      <td>0.08</td>\n",
       "      <td>0.2871</td>\n",
       "      <td>-0.2050</td>\n",
       "      <td>当前级别 (MMD: ['3sell'] Loss: 4904.0)</td>\n",
       "      <td>3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: Fals...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3buy</td>\n",
       "      <td>2022-02-22 10:55:00</td>\n",
       "      <td>2022-02-23 09:40:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>4959.0</td>\n",
       "      <td>19.9637</td>\n",
       "      <td>4916.0</td>\n",
       "      <td>0.10</td>\n",
       "      <td>0.9276</td>\n",
       "      <td>-0.5041</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4916.0)</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3buy</td>\n",
       "      <td>2022-02-24 21:05:00</td>\n",
       "      <td>2022-02-24 21:50:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>5185.0</td>\n",
       "      <td>19.0935</td>\n",
       "      <td>5096.0</td>\n",
       "      <td>-0.42</td>\n",
       "      <td>0.1350</td>\n",
       "      <td>-0.5593</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 5096.0)</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>l3buy</td>\n",
       "      <td>2022-02-25 01:25:00</td>\n",
       "      <td>2022-02-25 02:10:00</td>\n",
       "      <td>做多</td>\n",
       "      <td>5057.0</td>\n",
       "      <td>19.5768</td>\n",
       "      <td>5026.0</td>\n",
       "      <td>-0.16</td>\n",
       "      <td>0.0791</td>\n",
       "      <td>-0.2571</td>\n",
       "      <td>当前级别 (MMD: ['l3buy'] Loss: 5026.0)</td>\n",
       "      <td>l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>SHFE.ag2206</td>\n",
       "      <td>3sell</td>\n",
       "      <td>2022-02-25 02:10:00</td>\n",
       "      <td>2022-02-25 14:25:00</td>\n",
       "      <td>做空</td>\n",
       "      <td>5049.0</td>\n",
       "      <td>19.6078</td>\n",
       "      <td>5061.0</td>\n",
       "      <td>1.41</td>\n",
       "      <td>1.9014</td>\n",
       "      <td>-0.0396</td>\n",
       "      <td>当前级别 (MMD: ['3sell'] Loss: 5061.0)</td>\n",
       "      <td>3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: Fals...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           code    mmd        open_datetime       close_datetime type   price  \\\n",
       "0   SHFE.ag2206   3buy  2022-02-07 23:00:00  2022-02-08 09:00:00   做多  4781.0   \n",
       "1   SHFE.ag2206   3buy  2022-02-08 23:00:00  2022-02-09 00:00:00   做多  4822.0   \n",
       "2   SHFE.ag2206   3buy  2022-02-12 01:00:00  2022-02-12 01:50:00   做多  4816.0   \n",
       "3   SHFE.ag2206   3buy  2022-02-14 13:55:00  2022-02-15 11:20:00   做多  4904.0   \n",
       "4   SHFE.ag2206  l3buy  2022-02-14 21:05:00  2022-02-15 11:20:00   做多  4913.0   \n",
       "5   SHFE.ag2206  1sell  2022-02-15 11:20:00  2022-02-15 22:15:00   做空  4941.0   \n",
       "6   SHFE.ag2206  l3buy  2022-02-15 22:15:00  2022-02-16 00:25:00   做多  4797.0   \n",
       "7   SHFE.ag2206  3sell  2022-02-16 00:25:00  2022-02-16 00:55:00   做空  4793.0   \n",
       "8   SHFE.ag2206  l3buy  2022-02-16 00:40:00  2022-02-16 01:15:00   做多  4801.0   \n",
       "9   SHFE.ag2206  3sell  2022-02-16 01:15:00  2022-02-16 02:15:00   做空  4801.0   \n",
       "10  SHFE.ag2206   3buy  2022-02-17 22:35:00  2022-02-18 09:40:00   做多  4873.0   \n",
       "11  SHFE.ag2206  2sell  2022-02-18 21:50:00  2022-02-18 22:45:00   做空  4925.0   \n",
       "12  SHFE.ag2206   3buy  2022-02-18 22:45:00  2022-02-21 09:15:00   做多  4925.0   \n",
       "13  SHFE.ag2206  3sell  2022-02-21 13:40:00  2022-02-21 14:45:00   做空  4877.0   \n",
       "14  SHFE.ag2206   3buy  2022-02-22 10:55:00  2022-02-23 09:40:00   做多  4959.0   \n",
       "15  SHFE.ag2206   3buy  2022-02-24 21:05:00  2022-02-24 21:50:00   做多  5185.0   \n",
       "16  SHFE.ag2206  l3buy  2022-02-25 01:25:00  2022-02-25 02:10:00   做多  5057.0   \n",
       "17  SHFE.ag2206  3sell  2022-02-25 02:10:00  2022-02-25 14:25:00   做空  5049.0   \n",
       "\n",
       "     amount  loss_price  profit_rate  max_profit_rate  max_loss_rate  \\\n",
       "0   20.7070      4767.0         0.36           0.5438        -0.2510   \n",
       "1   20.5309      4783.0        -0.06           0.2489        -0.1866   \n",
       "2   20.5565      4799.0         0.08           0.3530        -0.0623   \n",
       "3   20.1876      4892.0         0.75           1.2235        -0.1223   \n",
       "4   20.1506      4898.0         0.57           1.0381        -0.3053   \n",
       "5   20.0364      4964.0         2.91           3.2584        -0.2429   \n",
       "6   20.6379      4780.0        -0.08           0.1042        -0.4169   \n",
       "7   20.6551      4802.0        -0.23           0.0209        -0.3130   \n",
       "8   20.6207      4777.0         0.00           0.1458        -0.0833   \n",
       "9   20.6207      4808.0        -0.21           0.0417        -0.2499   \n",
       "10  20.3160      4857.0         0.27           0.7182        -0.3078   \n",
       "11  20.1015      4945.0         0.00           0.4061        -0.0609   \n",
       "12  20.1015      4905.0        -0.71           0.1827        -0.8528   \n",
       "13  20.2994      4904.0         0.08           0.2871        -0.2050   \n",
       "14  19.9637      4916.0         0.10           0.9276        -0.5041   \n",
       "15  19.0935      5096.0        -0.42           0.1350        -0.5593   \n",
       "16  19.5768      5026.0        -0.16           0.0791        -0.2571   \n",
       "17  19.6078      5061.0         1.41           1.9014        -0.0396   \n",
       "\n",
       "                               open_msg  \\\n",
       "0    当前级别 (MMD: ['3buy'] Loss: 4767.0)    \n",
       "1    当前级别 (MMD: ['3buy'] Loss: 4783.0)    \n",
       "2    当前级别 (MMD: ['3buy'] Loss: 4799.0)    \n",
       "3    当前级别 (MMD: ['3buy'] Loss: 4892.0)    \n",
       "4   当前级别 (MMD: ['l3buy'] Loss: 4898.0)    \n",
       "5   当前级别 (MMD: ['1sell'] Loss: 4964.0)    \n",
       "6   当前级别 (MMD: ['l3buy'] Loss: 4780.0)    \n",
       "7   当前级别 (MMD: ['3sell'] Loss: 4802.0)    \n",
       "8   当前级别 (MMD: ['l3buy'] Loss: 4777.0)    \n",
       "9   当前级别 (MMD: ['3sell'] Loss: 4808.0)    \n",
       "10   当前级别 (MMD: ['3buy'] Loss: 4857.0)    \n",
       "11  当前级别 (MMD: ['2sell'] Loss: 4945.0)    \n",
       "12   当前级别 (MMD: ['3buy'] Loss: 4905.0)    \n",
       "13  当前级别 (MMD: ['3sell'] Loss: 4904.0)    \n",
       "14   当前级别 (MMD: ['3buy'] Loss: 4916.0)    \n",
       "15   当前级别 (MMD: ['3buy'] Loss: 5096.0)    \n",
       "16  当前级别 (MMD: ['l3buy'] Loss: 5026.0)    \n",
       "17  当前级别 (MMD: ['3sell'] Loss: 5061.0)    \n",
       "\n",
       "                                            close_msg  \n",
       "0   3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "1   3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "2   3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "3   3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, PZ...  \n",
       "4   l3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, P...  \n",
       "5   1sell 当前级别出现买点（MMDS: ['l3buy']，BC：（BI: False, ...  \n",
       "6   l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...  \n",
       "7                                            3sell 止损  \n",
       "8   l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...  \n",
       "9                                            3sell 止损  \n",
       "10  3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "11  2sell 当前级别出现买点（MMDS: ['3buy']，BC：（BI: False, P...  \n",
       "12                                            3buy 止损  \n",
       "13  3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: Fals...  \n",
       "14  3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "15  3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "16  l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...  \n",
       "17  3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: Fals...  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示历史持仓\n",
    "#设置显示全部行，不省略\n",
    "pd.set_option('display.max_rows',None)\n",
    "#设置显示全部列，不省略\n",
    "pd.set_option('display.max_columns',None)\n",
    "\n",
    "# 显示历史持仓\n",
    "pos_pd = BT.positions()\n",
    "pos_pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-05-04T11:16:28.185525Z",
     "iopub.status.busy": "2022-05-04T11:16:28.185525Z",
     "iopub.status.idle": "2022-05-04T11:16:28.212524Z",
     "shell.execute_reply": "2022-05-04T11:16:28.212524Z",
     "shell.execute_reply.started": "2022-05-04T11:16:28.185525Z"
    },
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>datetime</th>\n",
       "      <th>type</th>\n",
       "      <th>price</th>\n",
       "      <th>amount</th>\n",
       "      <th>info</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2022-02-07 23:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4781.0</td>\n",
       "      <td>20.7070</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4767.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2022-02-08 09:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4798.0</td>\n",
       "      <td>20.7070</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2022-02-08 23:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4822.0</td>\n",
       "      <td>20.5309</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4783.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2022-02-09 00:00:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4819.0</td>\n",
       "      <td>20.5309</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2022-02-12 01:00:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4816.0</td>\n",
       "      <td>20.5565</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4799.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2022-02-12 01:50:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4820.0</td>\n",
       "      <td>20.5565</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2022-02-14 13:55:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4904.0</td>\n",
       "      <td>20.1876</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4892.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2022-02-14 21:05:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4913.0</td>\n",
       "      <td>20.1506</td>\n",
       "      <td>当前级别 (MMD: ['l3buy'] Loss: 4898.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2022-02-15 11:20:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4941.0</td>\n",
       "      <td>20.1876</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, PZ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>2022-02-15 11:20:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4941.0</td>\n",
       "      <td>20.1506</td>\n",
       "      <td>l3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, P...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2022-02-15 11:20:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4941.0</td>\n",
       "      <td>20.0364</td>\n",
       "      <td>当前级别 (MMD: ['1sell'] Loss: 4964.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2022-02-15 22:15:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4797.0</td>\n",
       "      <td>20.0364</td>\n",
       "      <td>1sell 当前级别出现买点（MMDS: ['l3buy']，BC：（BI: False, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>2022-02-15 22:15:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4797.0</td>\n",
       "      <td>20.6379</td>\n",
       "      <td>当前级别 (MMD: ['l3buy'] Loss: 4780.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2022-02-16 00:25:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4793.0</td>\n",
       "      <td>20.6379</td>\n",
       "      <td>l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>2022-02-16 00:25:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4793.0</td>\n",
       "      <td>20.6551</td>\n",
       "      <td>当前级别 (MMD: ['3sell'] Loss: 4802.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>2022-02-16 00:40:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4801.0</td>\n",
       "      <td>20.6207</td>\n",
       "      <td>当前级别 (MMD: ['l3buy'] Loss: 4777.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>2022-02-16 00:55:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4804.0</td>\n",
       "      <td>20.6551</td>\n",
       "      <td>3sell 止损</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>2022-02-16 01:15:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4801.0</td>\n",
       "      <td>20.6207</td>\n",
       "      <td>l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>2022-02-16 01:15:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4801.0</td>\n",
       "      <td>20.6207</td>\n",
       "      <td>当前级别 (MMD: ['3sell'] Loss: 4808.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>2022-02-16 02:15:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4811.0</td>\n",
       "      <td>20.6207</td>\n",
       "      <td>3sell 止损</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2022-02-17 22:35:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4873.0</td>\n",
       "      <td>20.3160</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4857.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>2022-02-18 09:40:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4886.0</td>\n",
       "      <td>20.3160</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>2022-02-18 21:50:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4925.0</td>\n",
       "      <td>20.1015</td>\n",
       "      <td>当前级别 (MMD: ['2sell'] Loss: 4945.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2022-02-18 22:45:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4925.0</td>\n",
       "      <td>20.1015</td>\n",
       "      <td>2sell 当前级别出现买点（MMDS: ['3buy']，BC：（BI: False, P...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>2022-02-18 22:45:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4925.0</td>\n",
       "      <td>20.1015</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4905.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>2022-02-21 09:15:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4890.0</td>\n",
       "      <td>20.1015</td>\n",
       "      <td>3buy 止损</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>2022-02-21 13:40:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4877.0</td>\n",
       "      <td>20.2994</td>\n",
       "      <td>当前级别 (MMD: ['3sell'] Loss: 4904.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>2022-02-21 14:45:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4873.0</td>\n",
       "      <td>20.2994</td>\n",
       "      <td>3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: Fals...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>2022-02-22 10:55:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>4959.0</td>\n",
       "      <td>19.9637</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 4916.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>2022-02-23 09:40:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4964.0</td>\n",
       "      <td>19.9637</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>2022-02-24 21:05:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>5185.0</td>\n",
       "      <td>19.0935</td>\n",
       "      <td>当前级别 (MMD: ['3buy'] Loss: 5096.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>2022-02-24 21:50:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>5163.0</td>\n",
       "      <td>19.0935</td>\n",
       "      <td>3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>2022-02-25 01:25:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>5057.0</td>\n",
       "      <td>19.5768</td>\n",
       "      <td>当前级别 (MMD: ['l3buy'] Loss: 5026.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>2022-02-25 02:10:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>5049.0</td>\n",
       "      <td>19.5768</td>\n",
       "      <td>l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>2022-02-25 02:10:00</td>\n",
       "      <td>buy</td>\n",
       "      <td>5049.0</td>\n",
       "      <td>19.6078</td>\n",
       "      <td>当前级别 (MMD: ['3sell'] Loss: 5061.0)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>2022-02-25 14:25:00</td>\n",
       "      <td>sell</td>\n",
       "      <td>4978.0</td>\n",
       "      <td>19.6078</td>\n",
       "      <td>3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: Fals...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              datetime  type   price   amount  \\\n",
       "0  2022-02-07 23:00:00   buy  4781.0  20.7070   \n",
       "1  2022-02-08 09:00:00  sell  4798.0  20.7070   \n",
       "2  2022-02-08 23:00:00   buy  4822.0  20.5309   \n",
       "3  2022-02-09 00:00:00  sell  4819.0  20.5309   \n",
       "4  2022-02-12 01:00:00   buy  4816.0  20.5565   \n",
       "5  2022-02-12 01:50:00  sell  4820.0  20.5565   \n",
       "6  2022-02-14 13:55:00   buy  4904.0  20.1876   \n",
       "7  2022-02-14 21:05:00   buy  4913.0  20.1506   \n",
       "8  2022-02-15 11:20:00  sell  4941.0  20.1876   \n",
       "9  2022-02-15 11:20:00  sell  4941.0  20.1506   \n",
       "10 2022-02-15 11:20:00   buy  4941.0  20.0364   \n",
       "11 2022-02-15 22:15:00  sell  4797.0  20.0364   \n",
       "12 2022-02-15 22:15:00   buy  4797.0  20.6379   \n",
       "13 2022-02-16 00:25:00  sell  4793.0  20.6379   \n",
       "14 2022-02-16 00:25:00   buy  4793.0  20.6551   \n",
       "15 2022-02-16 00:40:00   buy  4801.0  20.6207   \n",
       "16 2022-02-16 00:55:00  sell  4804.0  20.6551   \n",
       "17 2022-02-16 01:15:00  sell  4801.0  20.6207   \n",
       "18 2022-02-16 01:15:00   buy  4801.0  20.6207   \n",
       "19 2022-02-16 02:15:00  sell  4811.0  20.6207   \n",
       "20 2022-02-17 22:35:00   buy  4873.0  20.3160   \n",
       "21 2022-02-18 09:40:00  sell  4886.0  20.3160   \n",
       "22 2022-02-18 21:50:00   buy  4925.0  20.1015   \n",
       "23 2022-02-18 22:45:00  sell  4925.0  20.1015   \n",
       "24 2022-02-18 22:45:00   buy  4925.0  20.1015   \n",
       "25 2022-02-21 09:15:00  sell  4890.0  20.1015   \n",
       "26 2022-02-21 13:40:00   buy  4877.0  20.2994   \n",
       "27 2022-02-21 14:45:00  sell  4873.0  20.2994   \n",
       "28 2022-02-22 10:55:00   buy  4959.0  19.9637   \n",
       "29 2022-02-23 09:40:00  sell  4964.0  19.9637   \n",
       "30 2022-02-24 21:05:00   buy  5185.0  19.0935   \n",
       "31 2022-02-24 21:50:00  sell  5163.0  19.0935   \n",
       "32 2022-02-25 01:25:00   buy  5057.0  19.5768   \n",
       "33 2022-02-25 02:10:00  sell  5049.0  19.5768   \n",
       "34 2022-02-25 02:10:00   buy  5049.0  19.6078   \n",
       "35 2022-02-25 14:25:00  sell  4978.0  19.6078   \n",
       "\n",
       "                                                 info  \n",
       "0                  当前级别 (MMD: ['3buy'] Loss: 4767.0)   \n",
       "1   3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "2                  当前级别 (MMD: ['3buy'] Loss: 4783.0)   \n",
       "3   3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "4                  当前级别 (MMD: ['3buy'] Loss: 4799.0)   \n",
       "5   3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "6                  当前级别 (MMD: ['3buy'] Loss: 4892.0)   \n",
       "7                 当前级别 (MMD: ['l3buy'] Loss: 4898.0)   \n",
       "8   3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, PZ...  \n",
       "9   l3buy 当前级别出现卖点（MMDS: ['1sell']，BC：（BI: True, P...  \n",
       "10                当前级别 (MMD: ['1sell'] Loss: 4964.0)   \n",
       "11  1sell 当前级别出现买点（MMDS: ['l3buy']，BC：（BI: False, ...  \n",
       "12                当前级别 (MMD: ['l3buy'] Loss: 4780.0)   \n",
       "13  l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...  \n",
       "14                当前级别 (MMD: ['3sell'] Loss: 4802.0)   \n",
       "15                当前级别 (MMD: ['l3buy'] Loss: 4777.0)   \n",
       "16                                           3sell 止损  \n",
       "17  l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...  \n",
       "18                当前级别 (MMD: ['3sell'] Loss: 4808.0)   \n",
       "19                                           3sell 止损  \n",
       "20                 当前级别 (MMD: ['3buy'] Loss: 4857.0)   \n",
       "21  3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "22                当前级别 (MMD: ['2sell'] Loss: 4945.0)   \n",
       "23  2sell 当前级别出现买点（MMDS: ['3buy']，BC：（BI: False, P...  \n",
       "24                 当前级别 (MMD: ['3buy'] Loss: 4905.0)   \n",
       "25                                            3buy 止损  \n",
       "26                当前级别 (MMD: ['3sell'] Loss: 4904.0)   \n",
       "27  3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: Fals...  \n",
       "28                 当前级别 (MMD: ['3buy'] Loss: 4916.0)   \n",
       "29  3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "30                 当前级别 (MMD: ['3buy'] Loss: 5096.0)   \n",
       "31  3buy 当前级别出现卖点（MMDS: []，BC：（BI: True, PZ: False...  \n",
       "32                当前级别 (MMD: ['l3buy'] Loss: 5026.0)   \n",
       "33  l3buy 当前级别出现卖点（MMDS: ['3sell']，BC：（BI: False, ...  \n",
       "34                当前级别 (MMD: ['3sell'] Loss: 5061.0)   \n",
       "35  3sell 当前级别出现买点（MMDS: []，BC：（BI: True, PZ: Fals...  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示历史持仓\n",
    "orders_pd = BT.orders()\n",
    "orders_pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 展示图表\n",
    "BT.show_charts(BT.codes[0], BT.frequencys[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "toc-autonumbering": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
